<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">
      <uni-forms-item name="name" label="商品名称" required>
        <input placeholder="请输入商品名称" @input="binddata('name', $event.detail.value)" class="uni-input-border" v-model="formData.name" trim="both"></input>
      </uni-forms-item>
      <uni-forms-item name="code" label="货号" required>
        <input placeholder="请输入货号" @input="binddata('code', $event.detail.value)" class="uni-input-border" v-model="formData.code" trim="both"></input>
      </uni-forms-item>
      <uni-forms-item name="price" label="价格" required>
        <input type="number" @input="binddata('price', $event.detail.value)" class="uni-input-border" v-model="formData.price"></input>
      </uni-forms-item>
      <uni-forms-item name="size" label="规格" required>
        <input placeholder="请输入规格" @input="binddata('size', $event.detail.value)" class="uni-input-border" v-model="formData.size" trim="both"></input>
      </uni-forms-item>
      <uni-forms-item name="address" label="地址" required>
        <input placeholder="请输入地址" @input="binddata('address', $event.detail.value)" class="uni-input-border" v-model="formData.address" trim="both"></input>
      </uni-forms-item>
      <uni-forms-item name="color" label="颜色">
        <input placeholder="请输入颜色" @input="binddata('color', $event.detail.value)" class="uni-input-border" v-model="formData.color"></input>
      </uni-forms-item>
      <uni-forms-item name="img" label="封面" required>
        <uni-file-picker limit="1" file-mediatype="image" return-type="object" v-model="formData.img"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="imgList" label="轮播图" required>
        <uni-file-picker limit="5" file-mediatype="image" :multiple="true" v-model="formData.imgList"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="imgListDetails" label="详情图" required>
        <uni-file-picker limit="10" file-mediatype="image" :multiple="true" v-model="formData.imgListDetails"></uni-file-picker>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" @click="submit">提交</button>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/index.js';

  const db = uniCloud.database();
  const dbCollectionName = 'index';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.indexOf(key) > -1) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "name": "",
        "code": "",
        "price": null,
        "size": "均码",
        "address": "",
        "color": "",
        "img": null,
        "imgList": [],
        "imgListDetails": []
      }
      return {
        formData,
        formOptions: {},
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            icon: 'none',
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>

<style>
  .uni-container {
    padding: 15px;
  }

  .uni-input-border,
  .uni-textarea-border {
    width: 100%;
    font-size: 14px;
    color: #666;
    border: 1px #e5e5e5 solid;
    border-radius: 5px;
    box-sizing: border-box;
  }

  .uni-input-border {
    padding: 0 10px;
    height: 35px;

  }

  .uni-textarea-border {
    padding: 10px;
    height: 80px;
  }

  .uni-button-group {
    margin-top: 50px;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    justify-content: center;
  }

  .uni-button {
    width: 184px;
  }
</style>
